<template>
    <div class="login">
        <el-form ref="loginRef" :model="loginForm" status-icon :rules="rules" label-width="auto" class="login-form">
            <img src="../assets/logo.png" class="logeImg" />
            <el-form-item label="" prop="username">
                <el-input v-model="loginForm.username" placeholder="账号" class="inputCss">
                    <template #prefix><img src="@/assets/images/login/user.png"
                            class="el-input__icon input-icon" /></template>
                </el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
                <el-input v-model="loginForm.password" type="password" show-password placeholder="密码" @keyup.enter="handleLogin"
                    class="inputCss">
                    <template #prefix><img src="@/assets/images/login/password.png"
                            class="el-input__icon input-icon" /></template>
                </el-input>
            </el-form-item>
            <el-form-item label="" prop="code" v-if="captchaEnabled">
                <el-input v-model="loginForm.code" placeholder="验证码" @keyup.enter="handleLogin" class="inputCss">
                    <template #prefix>
                        <img src="@/assets/images/login/code.png" class="el-input__icon input-icon" />
                    </template>
                </el-input>
                <div class="login-code">
                    <img :src="codeUrl" @click="getCode" class="login-code-img" />
                </div>
            </el-form-item>
            <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px; color: #fff;">记住密码</el-checkbox>
            <el-form-item>
                <el-button :loading="loading"  size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
                    <span v-if="!loading">登 录</span>
                    <span v-else>登 录 中...</span>
                </el-button>
                <!-- <el-button @click="resetForm">重置</el-button> -->
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt"; //RSA 是非对称的加密,只能校验加密出来的东西相不相等
import useUserStore from '@/store/user'
const userStore = useUserStore()
import { useRouter } from 'vue-router';
const router = useRouter();

const loginRef = ref(null)
const loginForm = ref({
    username: '',
    password: '',
    code: '',
    uuid: "",
    rememberMe: false
})

const rules = reactive({
    username: [{ required: true, message: '请输入您的账号', trigger: 'blur' }],
    password: [{ required: true, message: '请输入您的密码', trigger: 'blur' }],
    code: [{ required: true, trigger: "change", message: "请输入验证码" }]
})

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);

const handleLogin = () => {
    loginRef.value?.validate((valid) => {
        if (valid) {
            loading.value = true;
            // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
            if (loginForm.value.rememberMe) {
                Cookies.set("username", loginForm.value.username, { expires: 30 });
                Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
                Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
            } else {
                // 否则移除
                Cookies.remove("username");
                Cookies.remove("password");
                Cookies.remove("rememberMe");
            }
            userStore.login(loginForm.value).then(() => {
                router.push({ path: '/' })
            }).catch(() => {
                loading.value = false;
                // 重新获取验证码
                if (captchaEnabled.value) {
                    getCode();
                }
            });
        } else {
            console.log('error submit!')
        }
    })
}

const resetForm = () => {
    loginRef.value?.resetFields()
}

// 验证码
function getCode() {
    getCodeImg().then(res => {
        console.log(res);
        
        captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (captchaEnabled.value) {
            codeUrl.value = "data:image/gif;base64," + res.img;
            loginForm.value.uuid = res.uuid;
        }
    });
}

// 获取 Cookies 里的登录信息，回填
function getCookie() {
    const username = Cookies.get("username");
    const password = Cookies.get("password");
    const rememberMe = Cookies.get("rememberMe");
    loginForm.value = {
        username: username === undefined ? loginForm.value.username : username,
        password: password === undefined ? loginForm.value.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
    };
}

getCode()
getCookie();
</script>



<style lang='scss' scoped>
.login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../assets/images/login/login-background.jpg");
    background-size: cover;
}

.title {
    margin: 0px auto 30px auto;
    text-align: center;
    // color: #fff;
    background-image: -webkit-linear-gradient(top, #fff, #577d6e);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.login-form {
    border-radius: 6px;
    background: #29403b82;
    width: 420px;
    padding: 25px 25px 5px 25px;

    .el-input {
        height: 40px;

        input {
            height: 40px;
        }
    }

    .input-icon {
        height: 20px;
        width: 20px;
        margin-left: 0px;
    }
}

.login-code {
    width: auto;
    height: 40px;
    position: absolute;
    right: 0;
    top: 0;

    img {
        cursor: pointer;
        vertical-align: middle;
    }
}

.login-code-img {
    width: auto;
    height: 40px;
    padding-left: 12px;
}

.inputCss ::v-deep .el-input__wrapper {
    background-color: #2c3c38;
    box-shadow: 0 0 0 0px var(--el-input-border-color, var(--el-border-color)) inset;
    cursor: default;

    .el-input__inner {
        color: #fff;
        cursor: default !important;
    }
}

.logeImg{
  width: 70%;
  height: auto;
  margin: 0 auto;
  display: block;
  margin-bottom: 20px
}
</style>
